<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <style>
        #app{
            margin: 100px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
    </style>
    <div id="app">
        <label for="input">
            请输入信息：
            <input type="text" id="input">
        </label>
        <br>
        <div>
            绑定的数据：<span id="show"></span>
        </div>
        
    </div>

    <script type="text/javascript">
        var obj = {};
        document.getElementById('input').focus();
        Object.defineProperty(obj, 'txt', {
            //每次取值时候会调用
            get: function(){
                return document.getElementById('input').value;
            },
            //每次改变值时候会调用
            set: function(newValue){
                document.getElementById('input').value = newValue;
                document.getElementById('show').innerHTML = newValue;
            }
        })
        //设置默认值
        obj.txt = 'hello';
        document.getElementById('input').addEventListener('keyup', function(e){
            obj.txt = e.target.value;
        }, false)
        
    </script>
</body>

</html>